.TH std::monostate 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::monostate \- std::monostate

.SH Synopsis
   Defined in header <variant>
   struct monostate { };        \fI(since C++17)\fP

   Unit type intended for use as a well-behaved empty alternative in std::variant. In
   particular, a variant of non-default-constructible types may list std::monostate as
   its first alternative: this makes the variant itself default-constructible.

.SH Member functions

   constructor           trivial implicit default/copy/move constructor
   (implicitly declared) \fI(public member function)\fP
   destructor            trivial implicit destructor
   (implicitly declared) \fI(public member function)\fP
   operator=             trivial implicit copy/move assignment
   (implicitly declared) \fI(public member function)\fP

.SH Non-member functions

std::operator==, !=, <, <=, >, >=, <=>(std::monostate)

   constexpr bool operator==( monostate, monostate )    \fB(1)\fP \fI(since C++17)\fP
   noexcept { return true; }
   constexpr bool operator!=( monostate, monostate )
   noexcept { return false; }

   constexpr bool operator< ( monostate, monostate )
   noexcept { return false; }
   constexpr bool operator> ( monostate, monostate )                      \fI(since C++17)\fP
   noexcept { return false; }                                             \fI(until C++20)\fP
   constexpr bool operator<=( monostate, monostate )
   noexcept { return true; }
                                                        \fB(2)\fP
   constexpr bool operator>=( monostate, monostate )
   noexcept { return true; }
   constexpr std::strong_ordering operator<=>(
   monostate, monostate ) noexcept

   {                                                                      \fI(since C++20)\fP
       return std::strong_ordering::equal;

   }

   All instances of std::monostate compare equal.

   The <, <=, >, >=, and != operators are synthesized from operator<=>    \fI(since C++20)\fP
   and operator== respectively.

.SH Helper classes

std::hash<std::monostate>

   template <>                   \fI(since C++17)\fP
   struct std::hash<monostate>;

   Specializes the std::hash algorithm for std::monostate.

.SH Example


// Run this code

 #include <cassert>
 #include <iostream>
 #include <variant>

 struct S
 {
     S(int i) : i(i) {}
     int i;
 };

 int main()
 {
     // Without the monostate type this declaration will fail.
     // This is because S is not default-constructible.
     std::variant<std::monostate, S> var;
     assert(var.index() == 0);

     try
     {
         std::get<S>(var); // throws! We need to assign a value
     }
     catch(const std::bad_variant_access& e)
     {
         std::cout << e.what() << '\\n';
     }

     var = 42;
     std::cout << "std::get: " << std::get<S>(var).i << '\\n'
               << "std::hash: " << std::hex << std::showbase
               << std::hash<std::monostate>{}(std::monostate{}) << '\\n';
 }

.SH Possible output:

 std::get: wrong index for variant
 std::get: 42
 std::hash: 0xffffffffffffe19f

.SH See also

   constructor   constructs the variant object
                 \fI(public member function)\fP
